
package org.openeai.lite.esb.transformers;

import org.apache.log4j.Logger;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractTransformer;

public class HttpRequestToString extends AbstractTransformer
{

    private Logger log = Logger.getLogger(this.getClass());

    @SuppressWarnings("deprecation")
    public HttpRequestToString()
    {
        registerSourceType(String.class);
        setReturnClass(String.class);
    }

    @SuppressWarnings("unused")
    @Override
    protected Object doTransform(Object payload, String encoding) throws TransformerException
    {
        String param;
        String value;
        String[] params;

        if (payload instanceof String)
        {
            // parses something like http://localhost:8585/sms?stationid=73

            log.info("[TRANSFORMER] Parsing HTTP request");
            log.info("[TRANSFORMER] Payload: " + payload);

            String msg = payload.toString();
            msg = msg.substring(msg.indexOf('?') + 1);
            params = msg.split("&");
            // param = params[0].substring(params[0].indexOf('='));
            value = params[0].substring(params[0].indexOf('=') + 1);
            // log.info("[TRANSFORMER] request param: " + param);
            log.info("[TRANSFORMER] request value: " + value);

        }
        else
        {
            log.error("Cannot parse message payload!");
            value = "ERROR";
        }

        return value;

    }

}
